﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3
{
    public partial class DriverPaymentForm : Form
    {
        private String connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=NauferTransactionDemoDB.accdb";
       public  DriverPaymentBean driverpaymentbean = null;
        private bool nonNumberEntered = false;
        
        public DriverPaymentForm()
        {
            InitializeComponent();
        }
        public DriverPaymentForm(String JobCode)
        {
            InitializeComponent();
            txtjobCode.Text = JobCode;
        }
    /*  public DriverPaymentForm(BillDatabean billdatabean)
        {

            InitializeComponent();

            txtjobCode.Text = billdatabean.JobCode;

            txtjobCode.Enabled = false;
            btnLater.Enabled = true;
            btnCancel.Enabled = false;


        }
     */
        
        public DriverPaymentForm(DriverPaymentBean driverpaymentbean)
        {
            InitializeComponent();
          
            fillForm(driverpaymentbean);
            showallPaymentbyjobcode();
            revievetotal();
            addtotalpaid();
            
            

        }

        private void DriverPaymentForm_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {


            addaction();



        }



        /// <summary>
        /// ADD A NEW PAYMENT
        /// </summary>
        public void addaction()
        {
            if (validationcontrol())
            {
                driverpaymentbean = null;

                driverpaymentbean = insertdatapayment();

                if (driverpaymentbean != null)
                {
                    insertintDBpaydata(driverpaymentbean);
                    MessageBox.Show("sucess");
                    clearcontrols();
                }
            }
        }


        /// <summary>
        /// INSERT DATA INTO DATABEAN FOR INSERTING
        /// </summary>
        /// <returns></returns>
        public DriverPaymentBean insertdatapayment()
        {

            driverpaymentbean = new DriverPaymentBean();
            driverpaymentbean.Jobcode = txtjobCode.Text;
            driverpaymentbean.Acceptorname = txtAcceptorName.Text;
            driverpaymentbean.Amount = float.Parse(mxtAmount.Text.ToString());
            driverpaymentbean.Postdate = dtppostdate.Value;
            driverpaymentbean.Description1 = rtxtDescription.Text;
            driverpaymentbean.Vehicleno = txtvehicleno.Text;
            if (rbtcheque.Checked)
            {
                driverpaymentbean.Chequeno = txtchequeno.Text;
                driverpaymentbean.Bankname = txtBankName.Text;
                driverpaymentbean.Chequedate = dtpchequedate.Value;
                driverpaymentbean.Chequereleasedate = dtpreleasingdate.Value;

                driverpaymentbean.Iscashselected = false;
                driverpaymentbean.Ischequeselected = true;
            }
            else if (rbtCash.Checked)
            {
                driverpaymentbean.Chequeno = "";
                driverpaymentbean.Bankname = "";

                driverpaymentbean.Iscashselected = true;
                driverpaymentbean.Ischequeselected = false;

            }



            return driverpaymentbean;
        }


        /// <summary>
        /// INSERT DATA INTO DATABASE
        /// </summary>
        /// <param name="driverpaymentbean"></param>
        public void insertintDBpaydata(DriverPaymentBean driverpaymentbean)
        {



            try
            {

                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                oleDbConnection1.Open();

                OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO driverpaymentmastertable (jobcode ,drivername,amount,chequeno,chequereleasedate,postdate,chequedate,ischeque,iscash,description,vehicleno) Values ('" + driverpaymentbean.Jobcode.ToString() + "','" + driverpaymentbean.Acceptorname.ToString() + "'," + driverpaymentbean.Amount + ",'" + driverpaymentbean.Chequeno + "','" + driverpaymentbean.Chequereleasedate + "','" + driverpaymentbean.Postdate + "','" + driverpaymentbean.Chequedate + "'," + driverpaymentbean.Ischequeselected + "," + driverpaymentbean.Iscashselected + ",'" + driverpaymentbean.Description1 + "','" + txtvehicleno.Text + "') ", oleDbConnection1);
                oleDbCommand1.CommandType = CommandType.Text;
                oleDbCommand1.ExecuteNonQuery();

                oleDbConnection1.Close();
            }
            catch (Exception e)
            {

                MessageBox.Show("Exception at Database close any Access Database program and restart" + e);
                Console.WriteLine("Error", e);
            }






        }


        public void clearcontrols()
        {
            txtAcceptorName.Text = "";
            txtBankName.Text = "";
            txtchequeno.Text = "";
            txtjobCode.Text = "";
            mxtAmount.Text = "";
            btnCancel.Enabled = true;


        }



        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
            
        }

        /// <summary>
        /// VALIDATES ALL CONTROL
        /// </summary>
        /// <returns></returns>
        public Boolean validationcontrol()
        {
            Boolean sucess = true;
            if (txtjobCode.Text.Trim() == null || txtjobCode.Text.Trim() == "")
            {

                txtjobCode.Focus();
                txtjobCode.Text = "";
                sucess = false;
                lblstatus.Text = "Enter jobcode";
            }
            else if (txtAcceptorName.Text.Trim() == null || txtAcceptorName.Text.Trim() == "")
            {

                txtAcceptorName.Focus();
                txtAcceptorName.Text = "";
                sucess = false;
                lblstatus.Text = "EnterAcepptor Name";
            }
            else if (txtvehicleno.Text.Trim() == null || txtvehicleno.Text.Trim() == "")
            {

                txtvehicleno.Focus();
                txtvehicleno.Text = "";
                sucess = false;
                lblstatus.Text = "Enter vehicle No";
            }
            else if (mxtAmount.Text.Trim() == null || mxtAmount.Text.Trim() == "")
            {

                mxtAmount.Focus();
                mxtAmount.Text = "";
                sucess = false;
                lblstatus.Text = "Enter Acceptor Name";
            }
            else if (rbtcheque.Checked)
            {
                if (txtchequeno.Text.Trim() == null || txtchequeno.Text.Trim() == "")
                {

                    txtchequeno.Focus();
                    txtchequeno.Text = "";
                    sucess = false;
                    lblstatus.Text = "Enter Cheque No";
                }
                if (txtBankName.Text.Trim() == null || txtBankName.Text.Trim() == "")
                {

                    txtBankName.Focus();
                    txtBankName.Text = "";
                    sucess = false;
                    lblstatus.Text = "Enter Bank Name";
                }


            }




            return sucess;
        }





        private void rbtcheque_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtcheque.Checked)
            {
                txtBankName.Enabled = true;
                txtchequeno.Enabled = true;
                dtpchequedate.Enabled = true;
                dtpreleasingdate.Enabled = true;

            }
            else
            {
                txtBankName.Enabled = false;
                txtchequeno.Enabled = false;
                dtpchequedate.Enabled = false;
                dtpreleasingdate.Enabled = false;
            }
        }

        private void mxtAmount_KeyDown(object sender, KeyEventArgs e)
        {
            nonNumberEntered = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace.
                    if (e.KeyCode != Keys.Back)
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        nonNumberEntered = true;
                    }

                }
            }
        }
        private void mxtAmount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (nonNumberEntered == true)
            {

                e.Handled = true;
                lblstatus.Text = "Enter TotalAmount Correctly";

            }

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            clearcontrols();
        }


        //fill the dform with data taken fronm the database
        // here the usercan edit the Data
        public void fillForm(DriverPaymentBean driverpaymentbean)
        {
            txtjobCode.Text = driverpaymentbean.Jobcode;
            txtjobCode.Enabled = false;

            
            txtAcceptorName.Text = driverpaymentbean.Acceptorname;

           
            txtvehicleno.Text = driverpaymentbean.Vehicleno;
           
        }

        private void btnLater_Click(object sender, EventArgs e)
        {

            txtAcceptorName.Text = "-";
            txtBankName.Text = "-";
            txtchequeno.Text = "-";
            txtvehicleno.Text = "-";
            mxtAmount.Text = "0";
            addaction();
            btnCancel.Enabled = true;


        }
        /// <summary>
        /// TO SEARCH AND GET A JOBCODE
        /// GOES TO JOBCODESEARCHFORM
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnsearch_Click(object sender, EventArgs e)
        {
            SearcJobCode searchjobCode = new SearcJobCode();
            searchjobCode.Show();
            this.Close();
            this.Dispose();
        }
        /// <summary>
        /// DISPLAY ALL PAYMENT AGAINST JOBCODE
        /// </summary>
        public void showallPaymentbyjobcode()
        {
            try
            {



                String query = "Select jobcode ,vehicleno,amount,postdate,description,ischeque,iscash from driverpaymentmastertable  where jobcode Like '" + txtjobCode.Text + "'  Order by postdate";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                DataSet ds = new DataSet();
                dAdapter.Fill(ds, "tblpayview");
                tblpaymentview.DataSource = ds.Tables["tblpayview"].DefaultView;
                if (ds.Tables.Count <= 0)
                {
                    lblstatus.Text = "No Payment Details Present";
                }
                
            }
            catch (Exception)
            {
                MessageBox.Show("The application had met with some errors please restart  the application :\n error:closer MSAccess files");

            }


        }
        /// <summary>
        ///ADD ALL PAID AMOUNT AND DISPLAY IN A LABEL
        /// </summary>
        public void addtotalpaid()
        {
           double  sum = 0;
           double balance = 0;
           double amountpayable = double.Parse(lbldriveramount.Text.ToString());
            int count = tblpaymentview.RowCount;
            if (count != 0)
            {

                for (int i = 0; i < count; i++)
                {
//
                    if (tblpaymentview.Rows[i].Cells[3].Value != null)
                    {
                        sum = sum + double.Parse(tblpaymentview.Rows[i].Cells[2].Value.ToString());
                    }
                }
                balance = amountpayable - sum;


                lblstatus.Text = sum.ToString();
                lblbalance.Text = balance.ToString();
                
            }






        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
      
        
       public  void revievetotal()
        {
           int driveramounttotal=0;

           String query = "Select * from driverassignmastertable where jobcode= '" + txtjobCode.Text + "' ";
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
            DataSet ds = new DataSet();
            dAdapter.Fill(ds, "driverassignm");
            if (ds.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("No quotation Confirmed against this Jobcode");
            }
            else
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    int a=int.Parse(driveramounttotal + ds.Tables[0].Rows[i][6].ToString());
                    driveramounttotal = driveramounttotal + a;
                }
                lbldriveramount.Text = driveramounttotal.ToString();
            }


        }


      


    }
}
